
import 'dart:convert';
import 'package:bt_tool/basic/bt_controller.dart';
import 'package:bt_tool/basic/bt_encrypt_tool.dart';
import 'package:bt_tool/basic/bt_utils.dart';
import 'package:bt_tool/widget/bt_text_field.dart';
import 'package:bt_tool/widget/bt_toast.dart';
import 'package:crypto/crypto.dart';
import 'package:get/get.dart';

enum EncryptAction{
  encrypt(0), //加密
  decrypt(1); //解密
  final int iValue;
  const EncryptAction(this.iValue);
  static EncryptAction fromInt(int value) {
    switch (value) {
      case 0:
        return EncryptAction.encrypt;
      case 1:
        return EncryptAction.decrypt;
      default:
        return EncryptAction.decrypt; // 或者抛出异常
    }
  }
}

enum EncryptType{
  base64(0),
  md5(1),
  des3(2);
  final int iValue;
  const EncryptType(this.iValue);
  static EncryptType fromInt(int value) {
    switch (value) {
      case 0:
        return EncryptType.base64;
      case 1:
        return EncryptType.md5;
      case 2:
        return EncryptType.des3;
      default:
        return EncryptType.base64; // 或者抛出异常
    }
  }
}

class EncryptMainController extends BTController{

  final BTTextFieldController inputCtl = BTTextFieldController();
  final BTTextFieldController resultCtl = BTTextFieldController();
  var actionType = EncryptAction.encrypt.obs;
  var cryptType = EncryptType.base64.obs;
  final BTTextFieldController des3KeyCtl = BTTextFieldController();
  final BTTextFieldController des3ivCtl = BTTextFieldController();

  @override
  dispose() {
    inputCtl.dispose();
    resultCtl.dispose();
    des3KeyCtl.dispose();
    des3ivCtl.dispose();
    super.dispose();
  }

  exchangeClick(){
    String str1 = btCommonSafeStr(inputCtl.textEditingController?.text);
    String str2 = btCommonSafeStr(resultCtl.textEditingController?.text);
    inputCtl.textEditingController?.text = str2;
    resultCtl.textEditingController?.text = str1;
  }

  action(){
    if(cryptType.value == EncryptType.base64){
      base64Action();
      return;
    }

    if(cryptType.value == EncryptType.md5){
      md5Action();
      return;
    }

    if(cryptType.value == EncryptType.des3){
      des3Action();
      return;
    }

  }

  base64Action(){
    String content = inputCtl.textEditingController!.text;
    if(content.isEmpty) {
      return;
    }

    if(actionType.value == EncryptAction.encrypt){
      try {
        resultCtl.textEditingController!.text =  base64.encode(utf8.encode(content));
      } catch (e) {
        BTToast.showToast(e.toString());
      }
      return;
    }

    try {
      resultCtl.textEditingController!.text =  utf8.decode(base64.decode(content));
    } catch (e) {
      BTToast.showToast(e.toString());
    }
  }

  md5Action(){
    if(actionType.value == EncryptAction.decrypt){
      BTToast.showToast("md5 不支持逆向");
      return;
    }

    String content = inputCtl.textEditingController!.text;
    if(content.isEmpty) {
      return;
    }

    try {
      resultCtl.textEditingController!.text =  md5.convert(utf8.encode(content)).toString();
    } catch (e) {
      BTToast.showToast(e.toString());
    }


  }

  des3Action() async {
    String content = inputCtl.textEditingController!.text;
    if(content.isEmpty){
      return;
    }
    String key = des3KeyCtl.textEditingController!.text;
    if(key.isEmpty){
      BTToast.showToast("请输入key");
      return;
    }

    String ivStr = des3ivCtl.textEditingController!.text;
    if(ivStr.isEmpty){
      BTToast.showToast("请输入iv");
      return;
    }

    if(actionType.value == EncryptAction.encrypt){
      try {
        String resultContent = await BTEncryptTool.encryptDES(content, key,iv: ivStr);
        resultCtl.textEditingController!.text = resultContent;
      } catch (e) {
        BTToast.showToast(e.toString());
      }
      return;
    }

    try {
      String resultContent = await BTEncryptTool.decryptDES(content, key,iv: ivStr);
      resultCtl.textEditingController!.text = resultContent;
    } catch (e) {
      BTToast.showToast(e.toString());
    }


  }

}